var weatherKinds = {
    '阴':'background-position:-475px -85px;',
    '多云':'background-position:-475px -85px;',

    '雨':'background-position:-85px -220px;',
    '晴':'background-position:-85px -85px;'
};
var cloudy = require('../img/cloudy.jpg')
var sunny = require('../img/image.jpg')
var rain = require('../img/rainny.jpg')
var sunny = require('../img/sunny.jpg')
var windy = require('../img/windy.jpg')
var weatherBackground ={
    '阴':cloudy,
    '晴':sunny,
    '雨':rain,
    "多云":windy
}
var map = new AMap.Map('', {
    resizeEnable: true,
});
AMap.plugin('AMap.Weather', function () {
    var weather = new AMap.Weather();
    AMap.plugin('AMap.CitySearch', function () {  //自动索取ip地图定位，即你的网络ip是哪，地图中心就会初始化到哪
        var citySearch = new AMap.CitySearch()
        citySearch.getLocalCity(function (status, result) {
            if (status === 'complete' && result.info === 'OK') {
                var city = result['city']; //获取城市信息
                weather.getLive(city, function (err, data) {
                    if (!err) {
                        var str = [];
                        str.push('<h4 >实时天气' + '</h4><hr>');
                        str.push('<p>城市/区：' + data.city + '</p>');
                        str.push('<p>天气：' + data.weather + '</p>');
                        str.push('<p>温度：' + data.temperature + '℃</p>');
                        str.push('<p>风向：' + data.windDirection + '</p>');
                        str.push('<p>风力：' + data.windPower + ' 级</p>');
                        str.push('<p>空气湿度：' + data.humidity + '</p>');
                        str.push('<p>发布时间：' + data.reportTime + '</p>');
                    }
                    document.getElementById('weather').innerHTML='<div class="inf-weather" style="position:inherit;margin-bottom:0;">'+str.join('')+'</div><div class="sharp"></div>'
                    var imagePath = '../img/'+weatherBackground[data.weather];
                    console.log(data.weather)
                     document.getElementById('forecast-icon').style.cssText=weatherKinds[data.weather];
                    document.getElementById('body').style.cssText = 'background-image: url("'+weatherBackground[data.weather]+'");'
                });
                //未来4天天气预报
                weather.getForecast(city, function (err, data) {
                    if (err) {
                        return;
                    }
                    var str = [];
                    for (var i = 0, dayWeather; i < data.forecasts.length; i++) {
                        dayWeather = data.forecasts[i];
                        str.push(dayWeather.date + ' <p>' + dayWeather.dayWeather  + dayWeather.nightTemp + '~' + dayWeather.dayTemp + '℃'+ '</p> ');
                    }
                    document.getElementById('forecast').innerHTML = str.join('<br>');
                });
            }
        })
    })
});

//显示未来天气，也就forecast
var forcastFlag = 0;
window.showForecast= function() {
    var forcast = document.getElementById('forecast');
    if (forcastFlag == 0){
        forcast.style.display = 'block';
        forcastFlag  = 1;
    }else {
        forcast.style.display = 'none';
        forcastFlag = 0;
    }
}

//显示天气图标